package jannex.sql.extensions;

import jannex.Jannex;
import jannex.mapping.ResultSetHandler;
import jannex.internal.ExceptionFactory;
import jannex.sql.JxResultSet;
import jannex.sql.TypedResultSet;
import jannex.sql.TypedStatement;

import java.sql.SQLException;
import java.sql.Statement;

public class TypedStatementImpl<E> extends StatementImpl implements TypedStatement<E> {

    private final ResultSetHandler<E> handler;

    public TypedStatementImpl(Statement statement, Jannex jannex, ResultSetHandler<E> handler) {
        super(statement, jannex);
        this.handler = handler;
    }


    @Override
    public TypedResultSet<E> executeQuery(String sql) {
        try {
            return new TypedResultSetImpl<>(statement.executeQuery(sql), jannex, handler);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public TypedResultSet<E> getResultSet() {
        try {
            return new TypedResultSetImpl<>(statement.getResultSet(), jannex, handler);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public JxResultSet getGeneratedKeys() {
        try {
            return new TypedResultSetImpl<>(statement.getGeneratedKeys(), jannex, handler);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }
}
